package com.qk.management.controller;

import com.qk.common.Result;
import com.qk.dto.user.UserDTO;
import com.qk.entity.User;
import com.qk.management.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;
    @DeleteMapping("/{ids}")
    public Result deleteUser(@PathVariable List<Integer> ids){
        log.info("删除用户{}",ids);
        userService.deleteUser(ids);
        return Result.success();
    }
    @PutMapping
    public Result updateUser(@RequestBody User user){
        log.info("更新用户{}",user);
        userService.updateUser(user);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getUser(@PathVariable Integer id){
        log.info("查询用户{}",id);
        User user =userService.getUser(id);
        return Result.success(user);
    }

    @PostMapping
    public Result addUser(@RequestBody User user){
        log.info("添加用户{}",user);
        userService.addUser(user);
        return Result.success();
    }
    @GetMapping
    public Result list(UserDTO userDTO){
        log.info("分页查询用户{}",userDTO);
        return userService.list(userDTO);
    }
    @GetMapping("/list")
    public Result selectAll(){
        List<User> users = userService.selectAll();
        return Result.success(users);
    }
    @GetMapping("/role/{roleLabel}")
    public Result selectByRoleId(@PathVariable String roleLabel){
        List<User> users = userService.selectByRoleId(roleLabel);
        return Result.success(users);
    }
}
